from win32com.client import constants, gencache
import os

def create_pdf(word_path, pdf_path):
    word = gencache.EnsureDispatch("Word.Application")
    doc = word.Documents.Open(word_path, ReadOnly=1)
    # 转换方法
    doc.ExportAsFixedFormat(pdf_path, constants.wdExportFormatPDF)
    word.Quit()

#
# create_pdf("E:\workspace\python\office\docx\文档介绍.docx", "E:\workspace\python\office\docx\文档介绍.pdf")

word_files = []
for file in os.listdir("."):
    if file.endswith(('.doc', '.docx')):
        word_files.append(file)
print(word_files)

for file in word_files:
    # 获取文件绝对路径
    word_path = os.path.abspath(file)
    index = word_path.rindex(".")
    pdf_path = word_path[:index] + ".pdf"
    create_pdf(word_path, pdf_path)
